/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.epsi.blokus.entity;

import java.awt.Color;
import org.epsi.blokus.joueur.Player;
import org.epsi.blokus.tools.FontsUtil;
import org.epsi.blokus.tools.IConstantes;
import org.epsi.blokus.tools.Log;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.state.StateBasedGame;


/**
 * Affiche le bloc Joueur
 * @author julien.lafont
 */
public class PlayerBox implements IEntity, IConstantes {

    // Polices utilisées
    private static UnicodeFont policeTitre = FontsUtil.getFontShadow("lte50335", 24, Color.WHITE, Color.BLACK, 1, 1, 1, false, false);
    private static UnicodeFont policeTime = FontsUtil.getFontShadow("lte50329", 24, Color.WHITE, Color.BLACK, 1, 1, 1, false, false);

    // Images partagérs
    private static Image imgYourTurn = null;
    private static Image imgHisTurn = null;


    private int x;
    private int y;
    private Player player;
    private Piece piece;

    // Différents états de l'avatar préchargés
    private Image avatars[] = new Image[AVATAR_T_ETATS.values().length];


    public PlayerBox(Player player, int x, int y)
    {
        this.player = player;
        this.x = x;
        this.y = y;
    }

    public void init(GameContainer container, StateBasedGame game) throws SlickException {

       Log.out("PlayerBox : init "+player.getPseudo());

       String avatar = player.getAvatar();
       for (AVATAR_T_ETATS etat : AVATAR_T_ETATS.values())
       {
            String path = AVATAR_FOLDER + "Portrait." + avatar+"."+etat.toString()+".png";
            avatars[etat.ordinal()] = new Image(path);
       }

       // Initialisation des ressources partagées
       if (imgHisTurn == null)
       {
           imgHisTurn = new Image("/res/Gui/HisTurnText.png");
           imgYourTurn = new Image("/res/Gui/YourTurnText.png");
       }

       piece  = new Piece(player.getCouleur(), this.x+105, y+155);

       
    }

    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
        if (player.getState() == INGAME_STATES.Play ) player.decreaseTimeLeft(delta);
        piece.update(container, game, delta);
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        this.avatars[player.getAvatarEtat().ordinal()].draw(this.x+0, this.y+82);

        // Affichage Pseudo
        g.setFont(policeTitre);
        g.drawString(
                player.getPseudo(),
                FontsUtil.getX_RightText(player.getPseudo(), policeTitre, this.x, 160),
                this.y);

        // Affichage Temps restant
        String timeleft = transformTimeleftToTime(player.getTimeleft());
        g.setFont(policeTime);
        g.drawString(
                timeleft,
                FontsUtil.getX_RightText(timeleft, policeTime, this.x, 160),
                this.y+30);

        // Affichage bouton Your Turn / His Turn
        if (player.itsMe() && player.isPlaying())
        {
            imgYourTurn.draw(this.x+100, this.y+90);
        }
        else if (!player.itsMe() && player.isPlaying())
        {
            imgHisTurn.draw(this.x+100, this.y+90);
        }

        piece.render(container, game, g);

    }


    /**
     * Transforme le temps restant (ms) en chaine de caractère
     * @param msLeft
     * @return
     */
    private static String transformTimeleftToTime(long msLeft)
    {
        if (msLeft<0) return "Timeout";
        
        int secondLeft = (int)msLeft/1000;
        int minutes = (int)Math.floor(secondLeft/60);
        int secondes = (int)secondLeft%60;

        StringBuilder sb = new StringBuilder();
        if (minutes<10) sb.append("0");
        sb.append(minutes).append(":");
        if (secondes<10) sb.append("0");
        sb.append(secondes);

        return sb.toString();
    }
}
